package com.hiwan.unions.activity.membership;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.ToastUtils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;

/**
 * Created by llg on 2017/7/11.
 */
public class MembershipRealActivity extends RootActivity implements HttpListener<JSONObject> {
    private EditText edit_card;
    private String card_real;
    Request<JSONObject> request;
    private String titles;

    public static void startPreview(Context context, String title) {
        Intent intent = new Intent(context, MembershipRealActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    protected int getLayout() {
        return R.layout.activity_mem_real;
    }

    @Override
    protected void initData() {
        bundle = getIntent().getExtras();
        titles = bundle.getString("title");
        setTitleTxt(titles);
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        edit_card = findView(R.id.edit_card);

    }


    @Override
    protected void IntentConfirm() {
        super.IntentConfirm();
        try {
            card_real = CheckUtils.checkReal(edit_card);
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.validaMemberStatus, RequestMethod.POST);
        request.add("member_card_id", card_real);
        CallServer.getRequestInstance().addToken(this, 0, request, this, true, userInfo);
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
        if (JsonUtil.getString(jsonObject, "memberStatus").equals("0")) {
            ToastUtils.showToast(this, "您已是工会会员");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // 结束刷新
                    com.hiwan.unions.activity.system.MembershipActivity.startPreview(MembershipRealActivity.this);
                    finish();
                }
            }, 3000);
        } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("1")) {
            MembershipshenheActivity.startPreview(this, titles);
        } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("2")) {
        } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("3")) {
            MembershipActivity.startPreview(this, card_real, titles, true);
        } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("9")) {
            MembershipSqActivity.startPreview(this, titles, card_real);
        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }
}
